/*
 * Copyright (C) 2009 Dimitrios C. Michalakos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.cs.util.date;

import java.sql.Timestamp;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * UnixTime can be used for calculating and converting Unix timestamps.
 * A Unix timestamp represents the seconds passed since the epoch (January 1, 1970 00:00:00 GMT).
 * @author dimitris@jmike.gr
 */
public class UnixTime {

    private final long ms;

    /**
     * Constructs a new Unix Time.
     * Automatically sets its value to the seconds passed since the epoch (January 1, 1970 00:00:00 GMT).
     */
    public UnixTime() {
        this.ms = System.currentTimeMillis();
    }

    /**
     * Constructs a new Unix Time
     * @param milliseconds the time passed since the epoch (January 1, 1970 00:00:00 GMT) in milliseconds.
     */
    public UnixTime(long milliseconds) {
        this.ms = milliseconds;
    }

    /**
     * Converts this Unix Time to Date.
     * @return
     */
    public java.util.Date getDate() {
        return new Date(ms);
    }

    /**
     * Converts this Unix Time to date.
     * @return
     */
    public java.sql.Timestamp getTimeStamp() {
        return new Timestamp(ms);
    }

    /**
     * Converts this Unix Time to String using the given pattern and the default date format symbols for the default locale.
     * @param pattern
     * @return 
     */
    public String getString(String pattern) {
        final SimpleDateFormat d = new SimpleDateFormat(pattern);
        final Date date = getDate();
        return d.format(date);
    }

    /** 
     * Converts Unix Time (seconds since 1970) to String using the given pattern and the default date format symbols for the given locale.
     * @param timestamp
     * @param pattern
     * @param locale
     * @return
     */
    public String getString(Long timestamp, String pattern, Locale locale) {
        final SimpleDateFormat d = new SimpleDateFormat(pattern, locale);
        final Date date = getDate();
        return d.format(date);
    }

    /** 
     * Converts Unix Time (seconds since 1970) to String using the given pattern and date format symbols.
     * @param timestamp
     * @param pattern
     * @param formatSymbols
     * @return
     */
    public String getString(Long timestamp, String pattern, DateFormatSymbols formatSymbols) {
        final SimpleDateFormat d = new SimpleDateFormat(pattern, formatSymbols);
        final Date date = getDate();
        return d.format(date);
    }
}
